package Stream;

import Lambda.Employee;
import org.junit.Before;
import org.junit.Test;

import java.net.Inet4Address;
import java.text.Collator;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo2 {

    private List<Employee> employeeList=null;

    @Before
    public void init(){
        employeeList=new ArrayList<>();
        employeeList.add(new Employee(1,"张三", 15,8000,"销售部", Employee.Status.BUSY));
        employeeList.add(new Employee(2,"李四", 35,6000,"客服", Employee.Status.FREE));
        employeeList.add(new Employee(3,"王五", 25,10000,"财务部", Employee.Status.VOCATION));
        employeeList.add(new Employee(4,"赵六", 35,11000,"财务部", Employee.Status.FREE));
        employeeList.add(new Employee(5,"田七", 22,12000,"管理部", Employee.Status.FREE));
        employeeList.add(new Employee(6,"老八", 26,15000,"技术部", Employee.Status.BUSY));
        employeeList.add(new Employee(7,"三炮", 27,9000,"技术部", Employee.Status.VOCATION));
        employeeList.add(new Employee(8,"李九", 29,11000,"管理部", Employee.Status.BUSY));
        employeeList.add(new Employee(9,"周十", 48,18000,"技术部", Employee.Status.VOCATION));
    }

    @Test
    public void test1(){
        employeeList.stream().map(Employee::getName).forEach(System.out::println);

        employeeList.stream().map((employee) -> employee.getAge() + 10).forEach(System.out::println);
    }

    @Test
    public void test2(){
        String []str=new String[]{"hello","world"};
        Arrays.stream(str).flatMap((s)->{
            List<Character> characterList=new ArrayList<>();
            for (Character c : s.toCharArray()) {
                characterList.add(c);
            }
            return characterList.stream();
        }).forEach(System.out::println);
    }

    @Test
    public void test3(){
        String []numStrArr=new String[]{"10","11","12","13","14"};

        Arrays.stream(numStrArr).mapToInt((s)->Integer.parseInt(s)).forEach(System.out::println);
    }

    @Test
    public void test4(){
        employeeList.stream().sorted((e1,e2)->{
            Collator instance = Collator.getInstance();
            return instance.getCollationKey(e1.getName()).compareTo(instance.getCollationKey(e2.getName()));
        }).forEach(System.out::println);
    }

    @Test
    public void test5(){
        boolean allMatchBUSY = employeeList.stream().allMatch((employee -> employee.getStatus() == Employee.Status.BUSY));
        System.out.println("是否所有的职工都处于BUSY状态? "+allMatchBUSY);
        boolean allMatchFREE = employeeList.stream().allMatch((employee -> employee.getStatus() == Employee.Status.FREE));
        System.out.println("是否所有的职工都处于FREE状态? "+allMatchFREE);
    }

    @Test
    public void test6(){
        boolean hasOrder40 = employeeList.stream().anyMatch((employee -> employee.getAge() > 40));
        System.out.println("有大于40岁的职工吗? "+hasOrder40);
        boolean hasOrder50 = employeeList.stream().anyMatch((employee -> employee.getAge() > 50));
        System.out.println("有大于50岁的职工吗? "+hasOrder50);
    }

    @Test
    public void test7(){
        boolean hasGrater15000 = employeeList.stream().noneMatch((emp) -> emp.getSalary() > 150000);
        System.out.println("有没有薪水大于15K的职工? " +hasGrater15000);
    }

    @Test
    public void test8(){
        employeeList.stream().sorted(Comparator.comparingInt(e -> (int) e.getSalary())).findFirst().ifPresent(System.out::println);
    }

    @Test
    public void test9(){
       employeeList.stream().findAny().ifPresent(System.out::println);
    }

    @Test
    public void test10(){
        int num[]=new int[]{1,2,3,4,5,6,7,8,9,10};
        int result = Arrays.stream(num).reduce(-55, (left, right) -> {
            System.out.println("left="+left+", right="+right+ ", left+right="+ (left+right));
            return left+right;
        });
        System.out.println(result);
    }

    @Test
    public void test11(){
        employeeList.stream().map(Employee::getSalary).reduce(Double::sum).ifPresent(System.out::println);
    }

    @Test
    public void test12(){
        List<String> nameList = employeeList.stream().map(Employee::getName).collect(Collectors.toList());
        nameList.forEach(System.out::println);

        //获取所有的部门，使用Set集合去重
        Set<String> departmentSet = employeeList.stream().map(Employee::getDepartment).collect(Collectors.toSet());
        departmentSet.forEach(System.out::println);

        LinkedList<String> nameLinkList = employeeList.stream().map(Employee::getName).collect(Collectors.toCollection(LinkedList::new));
        nameLinkList.forEach(System.out::println);
    }

    @Test
    public void test13(){
        Double meanSalary = employeeList.stream().map(Employee::getSalary).collect(Collectors.averagingDouble(salary -> salary));
        System.out.println("平均薪水为:"+ DecimalFormat.getInstance().format(meanSalary));

        Double sumSalary = employeeList.stream().collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println("总计共发工资为:"+sumSalary);

        Long num = employeeList.stream().collect(Collectors.counting());
        System.out.println("职工总数为:"+num);

        //年龄最大的
        employeeList.stream().map(Employee::getAge).collect(Collectors.maxBy(Comparator.comparingInt(age->age)))
        .ifPresent(System.out::println);

        //工资最少的
        employeeList.stream().collect(Collectors.minBy(Comparator.comparingDouble(e->e.getSalary()))).ifPresent(System.out::println);

    }

    @Test
    public void test14(){
        //Collectors.groupingBy 分组功能
        //按职工的状态分组
        employeeList.stream().collect(Collectors.groupingBy(Employee::getStatus)).forEach((k,v)->System.out.println(""+k+" = "+v));

        //按职工的部门分组
        employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment)).forEach((k,v)-> System.out.println(k+" = "+v));


        //多级分组
        Map<Employee.Status, Map<String, List<Employee>>> multiGroup = employeeList.stream().
                collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy(Employee::getDepartment)));
        StringBuilder sb=new StringBuilder();
        multiGroup.forEach((k,map)-> {
            sb.append(k+" = ");
            map.forEach((departName,list)->{
                sb.append("{"+departName+" = "+ list+"},");
            });
            System.out.println(sb.toString());
            sb.delete(0,sb.length());
        });
    }


    @Test
    public void test15(){
        Map<Boolean, List<Employee>> booleanListMap = employeeList.stream().collect(Collectors.partitioningBy(employee -> employee.getSalary() > 10000));
        booleanListMap.forEach((k,v)-> System.out.println(k+" = "+v));

    }

    @Test
    public void test16(){
        DoubleSummaryStatistics doubleSummaryStatistics = employeeList.stream().collect(Collectors.summarizingDouble(e -> e.getSalary()));
        long count = doubleSummaryStatistics.getCount();
        double average = doubleSummaryStatistics.getAverage();
        double max = doubleSummaryStatistics.getMax();
        double min = doubleSummaryStatistics.getMin();
        double sum = doubleSummaryStatistics.getSum();
    }

    @Test
    public void test17(){
        String nameStr = employeeList.stream().map(Employee::getName).collect(Collectors.joining(",", "前缀-", "-后缀"));
        System.out.println(nameStr);
    }


    @Test
    public void test18(){
        employeeList.stream().map(employee -> 1).reduce((x,y)->x+y).ifPresent(System.out::println);

    }



















}
